<HTML>
 
<!-- Mirrored from www.javapractices.com/apps/movies/javadoc/src-html/hirondelle/movies/login/LoginController.html by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:30:19 GMT -->
<HEAD>
  <TITLE>
LoginController.java
  </TITLE>
  <LINK REL ='stylesheet' TYPE='text/css' HREF='../../../../highlight.css' TITLE='Style'>
 </HEAD>
 <BODY>
<PRE>
<span class='keyword'>package</span> hirondelle.movies.login;<a name=line.1></a>
<a name=line.2></a>
<span class='keyword'>import</span> hirondelle.movies.main.MainWindow;<a name=line.3></a>
<span class='keyword'>import</span> java.util.logging.Logger;<a name=line.4></a>
<span class='keyword'>import</span> hirondelle.movies.util.Util;<a name=line.5></a>
<span class='keyword'>import</span> java.awt.event.ActionEvent;<a name=line.6></a>
<span class='keyword'>import</span> java.awt.event.ActionListener;<a name=line.7></a>
<a name=line.8></a>
<span class='comment'>/**<a name=line.9></a>
  Allow only authenticated users to log in to the application. <a name=line.10></a>
  <a name=line.11></a>
  &lt;P&gt;This implementation is only a toy. It doesn't perform realistic validation of user name and password.<a name=line.12></a>
  For your own applications, you will need to supply your own implementation.<a name=line.13></a>
*/</span><a name=line.14></a>
<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> LoginController <span class='keyword'>implements</span> ActionListener {<a name=line.15></a>
<a name=line.16></a>
  <span class='comment'>/**<a name=line.17></a>
   Listen for button clicks on the {@link LoginView} screen - {@link #LOGIN} or {@link #CANCEL}.<a name=line.18></a>
  */</span><a name=line.19></a>
  <span class='keyword'>public</span> <span class='keyword'>void</span> actionPerformed(ActionEvent aEvent) {<a name=line.20></a>
    String command = aEvent.getActionCommand();<a name=line.21></a>
    <span class='keyword'>if</span> ( LOGIN.equals(command) ) {<a name=line.22></a>
      validateUserCredentials();<a name=line.23></a>
    }<a name=line.24></a>
    <span class='keyword'>else</span> <span class='keyword'>if</span>( CANCEL.equals(command) ){<a name=line.25></a>
      shutDownApplication();<a name=line.26></a>
    }<a name=line.27></a>
    <span class='keyword'>else</span> {<a name=line.28></a>
      <span class='keyword'>throw</span> <span class='keyword'>new</span> AssertionError(<span class='literal'>"Unexpected command: "</span> + command);<a name=line.29></a>
    }<a name=line.30></a>
  }<a name=line.31></a>
<a name=line.32></a>
  <span class='comment'>/**<a name=line.33></a>
  Ask the end user for their user name and password.<a name=line.34></a>
  <a name=line.35></a>
  &lt;P&gt;This is the first screen displayed to the user upon startup.<a name=line.36></a>
  */</span><a name=line.37></a>
  <span class='keyword'>public</span> <span class='keyword'>void</span> askUserForCredentials(){<a name=line.38></a>
    fView = <span class='keyword'>new</span> LoginView(<span class='keyword'>this</span>);<a name=line.39></a>
    fView.showLoginScreen();<a name=line.40></a>
  }<a name=line.41></a>
  <a name=line.42></a>
  <span class='comment'>/**<a name=line.43></a>
   Validate the user name and password entered by the user.<a name=line.44></a>
   <a name=line.45></a>
   &lt;P&gt;If the validation succeeds, close the login screen and display the {@link MainWindow}.<a name=line.46></a>
   If the validation fails, allow the user to make a few more attempts at login.<a name=line.47></a>
   If the validation fails repeatedly, close the application.<a name=line.48></a>
  */</span><a name=line.49></a>
  <span class='keyword'>void</span> validateUserCredentials(){<a name=line.50></a>
    fNumAttempts++;<a name=line.51></a>
    String userName = fView.getUserName();<a name=line.52></a>
    String password = fView.getPassword();<a name=line.53></a>
    <span class='keyword'>if</span> ( isValid(userName, password) ){<a name=line.54></a>
      showMainWindow(userName);<a name=line.55></a>
      fView.close();<a name=line.56></a>
    }<a name=line.57></a>
    <span class='keyword'>else</span> {<a name=line.58></a>
      <span class='keyword'>if</span>( fNumAttempts &lt; MAX_NUM_ATTEMPTS) {<a name=line.59></a>
        fView.tryAgain();<a name=line.60></a>
      }<a name=line.61></a>
      <span class='keyword'>else</span> {<a name=line.62></a>
        fLogger.config(<span class='literal'>"Shutting down. User credentials not valid for more than the max number of tries."</span>);<a name=line.63></a>
        shutDownApplication();<a name=line.64></a>
      }<a name=line.65></a>
    }<a name=line.66></a>
  }<a name=line.67></a>
  <a name=line.68></a>
  <span class='comment'>/** Action command string. */</span><a name=line.69></a>
  <span class='keyword'>static</span> <span class='keyword'>final</span> String LOGIN = <span class='literal'>"Login"</span>;<a name=line.70></a>
  <a name=line.71></a>
  <span class='comment'>/** Action command string. */</span><a name=line.72></a>
  <span class='keyword'>static</span> <span class='keyword'>final</span> String CANCEL = <span class='literal'>"Cancel"</span>;<a name=line.73></a>
<a name=line.74></a>
   <span class='comment'>//  PRIVATE //<a name=line.75></a>
</span>  <span class='keyword'>private</span> LoginView fView;<a name=line.76></a>
  <span class='keyword'>private</span> <span class='keyword'>int</span> fNumAttempts = <span class='literal'>0</span>;<a name=line.77></a>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> <span class='keyword'>int</span> MAX_NUM_ATTEMPTS = <span class='literal'>3</span>;<a name=line.78></a>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> Logger fLogger = Util.getLogger(LoginController.<span class='keyword'>class</span>);<a name=line.79></a>
  <a name=line.80></a>
  <span class='comment'>/** <a name=line.81></a>
   Toy implementation. <a name=line.82></a>
   Login is always valid, &lt;em&gt;except&lt;/em&gt; the user name is &lt;tt&gt;'failme'&lt;/tt&gt;. This is <a name=line.83></a>
   to allow testing of the failure branch. <a name=line.84></a>
  */</span><a name=line.85></a>
  <span class='keyword'>private</span> <span class='keyword'>boolean</span> isValid(String aUserName, String aPassword){<a name=line.86></a>
    <span class='keyword'>return</span> Util.textHasContent(aUserName) &amp;&amp; ! <span class='literal'>"failme"</span>.equals(aUserName);<a name=line.87></a>
  }<a name=line.88></a>
  <a name=line.89></a>
  <span class='comment'>/** Display the app's main window to the user.  */</span><a name=line.90></a>
  <span class='keyword'>private</span> <span class='keyword'>void</span> showMainWindow(String aUserName){<a name=line.91></a>
    fLogger.config(<span class='literal'>"Showing the main window."</span>);<a name=line.92></a>
    MainWindow.getInstance().buildAndShow(aUserName);<a name=line.93></a>
  }<a name=line.94></a>
<a name=line.95></a>
  <span class='comment'>/** Close the login screen and exit the app. */</span><a name=line.96></a>
  <span class='keyword'>private</span> <span class='keyword'>void</span> shutDownApplication() {<a name=line.97></a>
    fView.close();<a name=line.98></a>
    System.exit(<span class='literal'>0</span>);<a name=line.99></a>
  }  <a name=line.100></a>
}<a name=line.101></a>
 <a name=line.102></a>
</PRE><a name=line.103></a>
 </BODY><a name=line.104></a>
<HTML><a name=line.105></a>
